package com.gitee.melin.bee.core.jdbc.dialect.impl;

import com.gitee.melin.bee.core.jdbc.enums.DataSourceType;
import com.gitee.melin.bee.core.jdbc.relational.ConnectionInfo;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TidbJdbcDialect extends MySqlJdbcDialect {

    protected static final List<String> EXCLUDED_SCHEMAS = Collections.unmodifiableList(
            Arrays.asList("information_schema", "mysql", "performance_schema", "metrics_schema"));

    @Override
    public DataSourceType getDataSourceType() {
        return DataSourceType.TIDB;
    }

    private static final Logger LOG = LoggerFactory.getLogger(TidbJdbcDialect.class);

    public TidbJdbcDialect(ConnectionInfo connectionInfo) {
        super(connectionInfo);
    }

    public TidbJdbcDialect(Connection connection) {
        super(connection);
    }

    @Override
    protected void connectionProperties(Map<String, String> connectionProperties) {
        connectionProperties.put("useInformationSchema", "true");
        connectionProperties.put("useUnicode", "true");
        connectionProperties.put("characterEncoding", "utf8");
        connectionProperties.put("tinyInt1isBit", "false");
        connectionProperties.put("rewriteBatchedStatements", "true");
        connectionProperties.put("connectTimeout", "10000"); // 10s
    }

    @Override
    protected Driver getDriver() throws SQLException {
        return loadDriver(DataSourceType.TIDB.getDriverClass());
    }

    @Override
    protected boolean filterSchema(String schemaName) {
        return !EXCLUDED_SCHEMAS.contains(schemaName);
    }
}
